package cn.jwcat.handler;

import cn.jwcat.utils.result.Result;
import cn.jwcat.utils.result.ResultCodeEnum;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ReExceptionHandler {
	//	org.springframework.dao.DuplicateKeyException
	@ExceptionHandler(value = {SignatureException.class, ExpiredJwtException.class})
	@ResponseBody
	public Result authorizationException(SignatureException e) {
		return Result.api(ResultCodeEnum.JWT_Malformed_ERROR, e.getMessage());
	}

	@ExceptionHandler
	public Result doExceptionHandler(Exception exception) {
		exception.printStackTrace();
		return Result.code(401).message("服务器故障,请稍后再试");
	}

	@ExceptionHandler(value = {HttpRequestMethodNotSupportedException.class})
	@ResponseBody
	public Result HttpRequestMethodNotSupportedException(SignatureException e) {
		return Result.api(ResultCodeEnum.HTTP_REQUESTMETHODS_NOTSUPPORTED, e.getMessage());
	}
}
